#include "board.h"
#include "ui_board.h"
#include <QPainter>
#include <QImage>
#include <QDebug>

Board::Board(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Board)
{
    ui->setupUi(this);
}

Board::~Board()
{
    delete ui;
}

void Board::convertPos(const int l_pos, const int n_pos, int &r_l_pos, int &r_n_pos)
{
    int One = 280;
    int stepNumber = -40;
    r_l_pos = l_pos*40;
    r_n_pos = One + (n_pos-1)*stepNumber;
}

void Board::convertPosToAbsolute(const int l_pos, const int n_pos, int &a_l_pos, int &a_n_pos)
{
    a_l_pos = l_pos / 40;
    a_n_pos = (n_pos-280)/(-40);
}

void Board::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.setPen(Qt::black);
    int c = 0;
    painter.fillRect(40,0,320,320, Qt::white);
    for(int i=40;i<360;i+=40)
    {
        painter.drawText(i+17, 340, QString('A'+c++));
        painter.drawText(20, i-17, QString::number(9-c));
    }
    painter.drawText(0, 0, QString('A'+c++));
    QImage bZ(":/items/res/black_zone.png");
    for(int i=40; i<360; i+=40)
    {
        for(int j=0; j<320;j+=40)
        {
            if((i/40)%2 == 1 && (j/40)%2 == 1)
                painter.drawImage(i,j,bZ);
            if((i/40)%2 == 0 && (j/40)%2 == 0)
                painter.drawImage(i,j,bZ);
            //pavoid Board::setupDefaultComplect()inter.drawText(i,j, QString::number(i).append(";").append(QString::number(j)));
        }
    }

    if ( (m_table.height() == 8) && ( m_table.width() == 8) )
    {
        for ( int c = 0; c < ChEngn::default_table_height; c++)
            for ( int r = 0; r < ChEngn::default_table_width; r++ )
            {
                ChEngn::Piece *tmp = m_table.pieceAt(c, r);
                if ( tmp->type() != ChEngn::unknown)
                {
                    int row, column;

                    convertPos( c+1, r+1, column, row);
                    painter.drawImage( column, row, getImageFor(tmp->type(), tmp->isWhite()));
                }
            }
    }

    painter.drawRect(40,0,320,320);
}

void Board::setTable(ChEngn::Table table)
{
    m_table = table;
    repaint();
}


QImage Board::getImageFor(ChEngn::piece_type type, bool isWhite)
{
    switch (type)
    {
        case ChEngn::pawn:
            if ( isWhite)
                return QImage(":/items/res/pawn_w.png");
            else
                return QImage(":/items/res/pawn_b.png");
            break;

        case ChEngn::knight:
            if ( isWhite)
                return QImage(":/items/res/knight_w.png");
            else
                return QImage(":/items/res/knight_b.png");
            break;

        case ChEngn::bishop:
            if ( isWhite)
                return QImage(":/items/res/bishop_w.png");
            else
                return QImage(":/items/res/bishop_b.png");
            break;

        case ChEngn::rook:
            if ( isWhite)
                return QImage(":/items/res/rook_w.png");
            else
                return QImage(":/items/res/rook_b.png");
            break;

        case ChEngn::queen:
            if ( isWhite)
                return QImage(":/items/res/queen_w.png");
            else
                return QImage(":/items/res/queen_b.png");
            break;

        case ChEngn::king:
            if ( isWhite)
                return QImage(":/items/res/king_w.png");
            else
                return QImage(":/items/res/king_b.png");
            break;
     }
    return QImage(":/items/res/black_zone.png");
}
